package com.lin.eat.serivce.impl;

import com.lin.eat.dao.ShoppingCarMapper;
import com.lin.eat.entry.ServerResponse;
import com.lin.eat.entry.ShoppingCar;
import com.lin.eat.entry.ShoppingCarDetail;
import com.lin.eat.serivce.ShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {

    @Autowired
    private ShoppingCarMapper shoppingCarMapper;

    /**
     * 加入购物车
     * @param shoppingCar
     * @return
     */
    @Override
    public ServerResponse addShoppingCar(ShoppingCar shoppingCar) {
        int i;
        ShoppingCar car = shoppingCarMapper.findShoppingCarByGid(shoppingCar.getG_id());
        i = car != null ? shoppingCarMapper.updateShoppingCarNumber(shoppingCar) : shoppingCarMapper.addShoppingCar(shoppingCar);
        ServerResponse<String> serverResponse = null;
        serverResponse = i>0 ? ServerResponse.createBySuccess(null) : ServerResponse.createByError(null);
        return serverResponse;
    }

    /**
     * 从购物车删除某商品
     * @param g_id
     * @return
     */
    @Override
    public ServerResponse deleteShoppingCar(String g_id) {
        int i = shoppingCarMapper.deleteShoppingCar(g_id);
        ServerResponse serverResponse = null;
        serverResponse = i>0 ? ServerResponse.createBySuccess(null) : ServerResponse.createByError(null);
        return serverResponse;
    }

    /**
     * 修改购物车中某个商品的选中状态
     * @param shoppingCar
     * @return
     */
    @Override
    public ServerResponse updateShoppingCarState(ShoppingCar shoppingCar) {
        int i = shoppingCarMapper.updateShoppingCarState(shoppingCar);
        ServerResponse serverResponse = null;
        serverResponse = i>0 ? ServerResponse.createBySuccess(null) : ServerResponse.createByError(null);
        return serverResponse;
    }

    /**
     * 修改购物车中所有商品的选中状态
     * @param s_state
     * @return
     */
    @Override
    public ServerResponse updateAllShoppingCarState(String s_state) {
        int i = shoppingCarMapper.updateAllShoppingCarState(s_state);
        ServerResponse serverResponse = null;
        serverResponse = i>0 ? ServerResponse.createBySuccess(null) : ServerResponse.createByError(null);
        return serverResponse;
    }

    /**
     * 获取购物车列表
     * @return
     */
    @Override
    public ServerResponse<List<ShoppingCarDetail>> getShoppingCarInfo() {
        List<ShoppingCarDetail> shoppingCarDetailList = shoppingCarMapper.getShoppingCarInfo();
        ServerResponse<List<ShoppingCarDetail>> serverResponse = null;
        serverResponse = shoppingCarDetailList.size()>0 ? ServerResponse.createBySuccess(shoppingCarDetailList) : ServerResponse.createByError(null);
        return serverResponse;
    }

    /**
     * 修改购物车某个商品的数量
     * @param shoppingCar
     * @return
     */
    @Override
    public ServerResponse updateShoppingCarNumber(ShoppingCar shoppingCar) {
        int i = shoppingCarMapper.updateShoppingCarNumber(shoppingCar);
        ServerResponse serverResponse = null;
        serverResponse = i>0 ? ServerResponse.createBySuccess(null) : ServerResponse.createByError(null);
        return serverResponse;
    }

    /**
     * 获取购物车中选中的商品
     * @return
     */
    @Override
    public ServerResponse<List<ShoppingCarDetail>> getShoppingCarChecked() {
        List<ShoppingCarDetail> shoppingCarDetailList = shoppingCarMapper.getShoppingCarChecked();
        ServerResponse<List<ShoppingCarDetail>> serverResponse = null;
        serverResponse = shoppingCarDetailList.size()>0 ? ServerResponse.createBySuccess(shoppingCarDetailList) : ServerResponse.createByError(null);
        return serverResponse;
    }

}
